//
//  HNSH_GroupApplyCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/18.
//

#import "HNSH_GroupApplyCell.h"

@interface HNSH_GroupApplyCell ()
@property (nonatomic, strong)UIImageView * imgView;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * contentLabel;
@property (nonatomic, strong)UIButton * btn;
@end

@implementation HNSH_GroupApplyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.contentView addSubview:self.imgView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.btn];
}

- (void)addAllLayout{
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(38);
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(self.contentView);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgView.mas_right).offset(11);
        make.right.mas_equalTo(-80);
        make.height.mas_equalTo(22);
        make.top.mas_equalTo(16);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom);
        make.height.mas_equalTo(17);
    }];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(48);
        make.height.mas_equalTo(24);
        make.centerY.mas_equalTo(self.contentView);
    }];
}

- (void)btnAction{
    if (self.acceptBlock) {
        self.acceptBlock(self);
    }
}

//MARK: Setter Methods
- (void)setModel:(HNSH_GroupApplyModel *)model{
    _model = model;
    
    HNSH_ChatFriendUserModel * user = model.user;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:user.portrait] placeholderImage:[UIImage imageNamed:@"base_user_header_icon"]];
    self.nameLabel.text = user.display_name.length ? user.display_name : user.name;
    self.contentLabel.text = model.message;
    if (model.status == 1) {
        self.btn.userInteractionEnabled = YES;
        [self.btn setTitle:@"确认" forState:UIControlStateNormal];
        [self.btn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        self.btn.backgroundColor = [UIColor appBlackLabelColor];
    } else {
        self.btn.userInteractionEnabled = NO;
        [self.btn setTitle:model.status == 2 ? @"已同意" : @"已过期" forState:UIControlStateNormal];
        [self.btn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        self.btn.backgroundColor = [UIColor clearColor];
    }
}

//MARK: Getter Methods
- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.layer.cornerRadius = 8;
        _imgView.layer.masksToBounds = YES;
        _imgView.image = [UIImage imageNamed:@"icon_placeholder"];
    }
    return _imgView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textColor = [UIColor appBlack333LabelColor];
    }
    return _nameLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont systemFontOfSize:12];
        _contentLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    }
    return _contentLabel;
}

- (UIButton *)btn{
    if (!_btn) {
        _btn = [UIButton new];
        [_btn setTitle:@"确认" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _btn.titleLabel.font = [UIFont systemFontOfSize:14];
        _btn.layer.cornerRadius = 6;
        _btn.layer.masksToBounds = YES;
        _btn.backgroundColor = [UIColor appBlackLabelColor];
        [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}

@end
